package com.wangmeng.book.server.action;

import com.wangmeng.book.dao.BaseDaoSupport;
import com.wangmeng.book.server.Command;
import com.wangmeng.book.server.CommandResult;
import com.wangmeng.book.utils.SpringContextManager;
import net.sf.json.JSONArray;
import org.apache.log4j.Logger;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: 王猛
 * Date: 13-9-24
 */
public class BookListAction implements IBaseAction {
    private static final Logger logger = Logger.getLogger(BookListAction.class);

    public CommandResult execute(Command cmd) {
        Map<String, Object> data = cmd.getData();
        if (data == null) {
            data = new HashMap<String, Object>();
        }
        Integer page = (Integer) data.get("page");
        Integer pageSize = (Integer) data.get("pageSize");
        List<Integer> status = (List<Integer>) data.get("status");
        Integer sourceType = (Integer) data.get("sourceType");
        String title = (String) data.get("title");
        String orderBy = (String) data.get("orderBy");
        CommandResult cr = new CommandResult();
        try {
            if (page == null || page <= 0) {
                page = 1;
            }
            if (pageSize == null || pageSize <= 0 || pageSize > 100) {
                pageSize = 20;
            }
            JdbcTemplate jt = SpringContextManager.getContext().getBean(JdbcTemplate.class);
            StringBuffer sql = new StringBuffer("SELECT oid,name,author,book_brief,status,word_num,href,view_count,cate_name,part_name,approve_status FROM `book_document` where 1=1 ");
            List param = new ArrayList();
            if (status != null && status.size() > 0) {
                sql.append(" and approve_status in (");
                for (int i = 0; i < status.size(); i++) {
                    if (i > 0) {
                        sql.append(",");
                    }
                    sql.append(status.get(i));
                }
                sql.append(")");
            }
            if (sourceType != null && sourceType > 0) {
                sql.append(" and source_type = ? ");
                param.add(sourceType);
            }
            if (title != null && title.length() > 0) {
                sql.append(" and name like ? ");
                param.add(title + "%");
            }
            if (orderBy != null && orderBy.trim().length() > 0) {
//                sql.append("order by view_count desc");
                sql.append("order by ").append(orderBy.trim());
            }
            sql.append(" limit ?,? ");
            param.add((page - 1) * pageSize);
            param.add(pageSize);
            List<Map<String, Object>> list = jt.queryForList(sql.toString(), param.toArray());
            Map<String, Object> result = new HashMap<String, Object>();
            result.put("data", JSONArray.fromObject(list).toString());
            cr.setData(result);
        } catch (Exception ex) {
            logger.error(ex);
            cr.setCode(CommandResult.CODE_COMMON_ERROR);
            cr.setMessage(ex.getMessage());
        }
        return cr;
    }
}
